#include("/common/_layout.html")
#@layout()
#define layoutHead()
	<title>栏目编辑</title>
#end
#define layoutBody()
	<body>
		<div class="wrap-container">
			<form class="layui-form" style="width: 90%;padding-top: 20px;">
				<input type="hidden" id="cat_id" name="cat_id">
				<input type="hidden" id="type_id" name="type_id">
					<div class="layui-form-item">
						<label class="layui-form-label">上级：</label>
						<div class="layui-input-block">
							<select id="parent_id" name="parent_id" lay-verify="required">
								<option value="0">作为一级栏目</option>
							</select>
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">栏目名称：</label>
						<div class="layui-input-block">
							<input type="text" name="cat_name" required lay-verify="required" placeholder="请输入栏目名称" autocomplete="off" class="layui-input">
						</div>

					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">排序：</label>
						<div class="layui-input-block">
							<input type="text" name="cat_sort" placeholder="请输入排序" autocomplete="off" class="layui-input">
						</div>

					</div>
								<div class="layui-form-item">
									<label class="layui-form-label">图标：</label>
									<div class="layui-input-block">
										<input type="hidden" id="cat_icon" name="cat_icon">
										<input type="file" name="file_cat_icon" lay-ext="jpeg|jpg|png|gif" lay-type="images" lay-title="上传图片" class="layui-upload-file">
										<div id="div_cat_icon" style="padding: 5px;"></div>
									</div>
								</div>
					<div class="layui-form-item">
						<label class="layui-form-label">关联课程：</label>
						<div class="layui-input-block">
							<input type="radio" name="link_course" value="1" title="关联" checked>
							<input type="radio" name="link_course" value="0" title="不关联">
						</div>

					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">状态：</label>
						<div class="layui-input-block">
							<input type="radio" name="cat_state" value="1" title="启用" checked>
							<input type="radio" name="cat_state" value="0" title="禁用">
						</div>

					</div>

					<div class="layui-form-item">
						<div class="layui-input-block">
							<button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo">立即提交</button>
							
						</div>
					</div>
				</form>
		</div>

		<script src="/static/js/jquery.formHelp.js" type="text/javascript" charset="utf-8"></script>
		<script>
			//Demo
			layui.use(['form', 'layer', 'laytpl', 'upload'], function() {
				var form = layui.form();
				var layer = layui.layer;
				var laytpl = layui.laytpl;
				form.render();		
					
				
				layui.upload({
					url: '/api/file/upload',
					success: function(res) {
						if (res.code == 0){
							var sourceId = res.data.paramid.replace("file_","");
							var ftype = $('input[name="' + res.data.paramid+'"]').attr("lay-type");
							
							$('#'+sourceId).val(res.data.src);
							
							formatUpFile(sourceId, res.data.src, ftype);
						}else{
							parent.alertMsg(res.msg);
						}
					}
				});
				
				function formatUpFile(elid, surl, ftype){
					if (surl == null || surl == "" || surl == "undefined"){
						return;
					}
					var shtml = "";
					if (ftype=="images"){
						shtml = '<a href="'+surl+'" target="_blank"><img src="'+surl+'" width="100" height="100"></a>';
					}else{
						shtml = '<a href="'+surl+'" target="_blank">'+surl.substr(surl.lastIndexOf('/')+1)+'</a>';
					}
					$('#div_'+elid).html(shtml);
				}
				
				//监听提交
				form.on('submit(formDemo)', function(formData) {					
					sendPost("/api/articlecat/save", formData.field, function(res) {
						layer.alert(res.msg, {
							title: '信息提示'
						}, function(idx){
							if (res.code==200){
								parent.refresh();
							}
							
						});
					});
					return false;
				});
				
				// 初始化上级菜单
				function filterListMenu(listdata, pid, level){
					var menu_node1 = listdata.filter(function (e) { return e.parent_id == pid; });
					for (var i = 0; i < menu_node1.length; i++) {
						var shtml = '<option value="'+menu_node1[i].cat_id+'">';
						for(j=0;j<level;j++){
							shtml += '&nbsp;&nbsp;&nbsp;&nbsp;';
						}
						if (level>0){
							shtml += '├─';
						}
						shtml += menu_node1[i].cat_name + '</option>';
						document.getElementById('parent_id').innerHTML += shtml;
						// 迭代
						filterListMenu(listdata, menu_node1[i].cat_id, level+1);
					}
				}
				
				// 页面初始化
				function page_init(){
					var iid = getUrlParam('id');
					var itype_id = getUrlParam('typeid');
					//console.log('itype_id:'+itype_id);
					if (iid===null || iid === 'undefined'){
						iid = 0;
					}
					if (itype_id != null){
						//console.log('setitype_id:'+itype_id);
						$('#type_id').val(itype_id);
					}
					// 加载初始化编辑数据
					sendGet("/api/articlecat/getOne", {
						id : iid,
						type_id: itype_id
					}, function(res) {
						//console.log(res);						
						// 上级
						if (res.hasOwnProperty("listdata")) {
							filterListMenu(res.listdata, 0, 0);
							
							var pid = getUrlParam('pid');
							if (pid!=null && pid != 'undefined'){
								$('#parent_id').val(pid);
							}
						}
						form.render('select');
						
						if (res.hasOwnProperty("onedata")) {
							$("form.layui-form").setForm(res.onedata);
							formatUpFile("cat_icon", res.onedata.cat_icon, "images");
						}
						form.render();
					});
				}
				
				page_init();
			});
		</script>
	</body>

#end